/**
 **************************************************************************************************************************
 * 
 **************************************************************************************************************************
 * @file RBVisibilityManager.h
 **************************************************************************************************************************/


#ifndef __RBVISIBILITYMANAGER_H__
#define __RBVISIBILITYMANAGER_H__

/**************************************************************************************************************************
 * Includes
 **************************************************************************************************************************/
#include "RBRefObject.h"
#include "RBPlane.h"
#include "RBHandle.h"
#include "stb.h"

/**************************************************************************************************************************
 * Forward declarations
 **************************************************************************************************************************/
class RBPlane;
class RBCamera;
RBDeclareHandle(RBEntity);
RBDeclareHandle(RBBVolume);

/**************************************************************************************************************************
 * Classes
 **************************************************************************************************************************/
class RBVisibilityManager : public RBRefObject
{
public:
                                RBVisibilityManager     ();
    virtual                    ~RBVisibilityManager     ();

	static const int max_sync_points = 8;

    virtual void                start_visibility		(const RBCamera* camera, uint32 sync_point /*0 to max_sync_points-1*/);
	void						sync_visibility_results	(std::vector<RBBVolumeHandle>* nodes, uint32 sync_mask);

    void                        AddEntity               (RBEntityHandle pEntity);
    
    void                        RemoveEntity            (RBEntityHandle pEntity);

protected:

	static const int max_objects_per_batch = 128;

	struct rb_intersect_params
	{
		int8 results[max_objects_per_batch];
		RBPlane frustum_planes[6];
		RBBVolumeHandle* boxes;
		uint32 start;
		uint32 count;
	};

	friend void* intersect_spheres_frustum(void* data);
		
	std::vector<RBBVolumeHandle> m_aComponents;
    std::vector<rb_intersect_params> m_params[max_sync_points];
	stb_sync m_sync_points[max_sync_points];

};

RBDeclareSmartPointer(RBVisibilityManager);

/**************************************************************************************************************************/

#endif // __RBVISIBILITYMANAGER_H__